package com.me {
	import com.me.components.Avatar;
	import com.me.components.BitImage;
	import com.me.components.EventGrid;
	import com.me.components.LocalImage;
	import com.me.components.MapResource;
	import com.me.components.PortalAvatar;
	import com.me.components.Scene;
	import com.me.obj.Building;
	import com.me.obj.Image;

	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;

	import mx.core.FlexGlobals;

	import spark.core.SpriteVisualElement;

	/**
	 *
	 * @author yw
	 *
	 * @Date 2011-9-16 上午09:35:45
	 */
	public class MapScene extends SpriteVisualElement {
		private static var _instance:MapScene;
		public var bg:Image;
		private var _backgroud:SpriteVisualElement;
		// 格子
		private var _grid:GridLayer;

		private var preX:int = 0;
		private var preY:int = 0;

		public static function getInstance():MapScene {
			if (_instance == null) {
				_instance = new MapScene(new SingletonEnforce());
			}
			return _instance;
		}

		public function MapScene(enforce:SingletonEnforce) {
			if (enforce == null) {
				throw new Error("MapEditorComponent can not be instance");
			}
			_backgroud = new SpriteVisualElement();
			addChild(_backgroud);
			this.mouseChildren = this.mouseEnabled = true;
			addEventListener(MouseEvent.MOUSE_DOWN, startDragHandler);
		}

		private function startDragHandler(e:MouseEvent):void {
			if (!FlexGlobals.topLevelApplication.mapDrag) {
				return;
			}
			addEventListener(Event.ENTER_FRAME, move);
			addEventListener(MouseEvent.MOUSE_UP, stopDrageHandler);
			preX = e.stageX;
			preY = e.stageY;
		}

		private function stopDrageHandler(e:MouseEvent):void {
			removeEventListener(Event.ENTER_FRAME, move);
			removeEventListener(MouseEvent.MOUSE_UP, stopDrageHandler);
		}

		private function move(e:Event):void {
			if (stage) {
				var deltaX:int = stage.mouseX - preX;
				var deltaY:int = stage.mouseY - preY;
				this.x += deltaX;
				this.y += deltaY;
				preX = stage.mouseX;
				preY = stage.mouseY;
				if (this.x > 0 || this.width <= stage.stageWidth) {
					this.x = 0;
				} else if (this.width > stage.stageWidth && this.x < -this.width + stage.stageWidth) {
					this.x = -this.width + stage.stageWidth;
				}
				if (this.y > 0 || this.height <= stage.stageHeight) {
					this.y = 0;
				} else if (this.height > stage.stageHeight && this.y < -this.height + stage.stageHeight) {
					this.y = -this.height + stage.stageHeight;
				}
			}
		}

		public function addEvent():void {
			_grid.addEventGrid();
		}

		public function loadBackgroud(back:File):void {
			if (bg && _backgroud.contains(bg)) {
				removeChild(bg);
			}
			bg = new Image(back.nativePath);
			_backgroud.addChild(bg);
		}

		public function loadScene(sur:File):void {
			var bi:Scene = new Scene(sur, function(e:MapResource):void {
				addScene(e);
			});
		}

		public function newEmptyMap(w:int, h:int, gw:int, gh:int):void {
			this.width = _backgroud.width = w;
			this.height = _backgroud.height = h;

			if (_grid && contains(_grid)) {
				removeChild(_grid);
				_grid.clear();
			}

			_grid = new GridLayer(this.width, this.height, gh);
			addChild(_grid)
		}

		private function addScene(sur:MapResource):void {
			var scene:Scene = sur as Scene;
//			_surface.addChild(scene.BMD_BACK);
		}

		public function loadNpc(sur:File):void {
			var bi:Avatar = new Avatar(sur, function(e:MapResource):void {
				addNpc(e);
			}, stopListener, startListener);
		}

		public function loadPortal(res:File):void {
			var portal:PortalAvatar = new PortalAvatar(res, function(e:MapResource):void {
				var d:Building = new Building(Building.TYPE_PORTAL, e.fileName, Building.LAYER_BUILDING);
				e.attatchment = d;
				e.x = 200;
				e.y = 300;
			}, stopListener, startListener);
		}

		private function stopListener():void {
			removeEventListener(MouseEvent.MOUSE_DOWN, startDragHandler);
		}

		private function startListener():void {
			addEventListener(MouseEvent.MOUSE_DOWN, startDragHandler);
		}

		public function loadBuild(sur:File):void {
			var bi:LocalImage = new LocalImage(sur, function(e:MapResource):void {
				addBuilding(e);
			}, stopListener, startListener);
		}

		private function addBuilding(res:MapResource):void {
			var d:Building = new Building(Building.TYPE_BUILDING, res.fileName, Building.LAYER_BUILDING);
			res.attatchment = d;
		}

		private function addNpc(res:MapResource):void {
			var d:Building = new Building(Building.TYPE_NPC, res.fileName, Building.LAYER_BUILDING);
			res.attatchment = d;
			res.x = 100;
			res.y = 300;
		}

		public function get grid():GridLayer {
			return _grid;
		}

		public function set gridVisible(v:Boolean):void {
			if (_grid) {
				_grid.visible = v;
			}
		}
	}
}

class SingletonEnforce {

}
